package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupInviteApplyDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupInviteApplyVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.*;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * Date 2019-01-20 20:49:52<br>
 * Description
 * 用户被邀请加入群的接口类
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_INVITE_ACTION_PUSH)
public class GroupInviteAction {

	@Resource
	private GroupMemberService groupMemberService;
	@Resource
	private GroupInviteService groupInviteService;
	@Resource
	private GroupManageService groupManageService;
	@Resource
	private GroupInviteApplyService groupInviteApplyService;
	@Resource
	private UserService userService;
	@Resource
	private GroupService groupService;

	/**
	 * Date 2019-01-27 10:51:05<br>
	 * Description 获取作为邀请人邀请入群的请求数量
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getInviteApplyCount(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto query) {
		query.setInviterUserId(socketSession.getKey());
		return new ResultMessage(R.param("count", groupManageService.countInviteRequestList(query)));
	}

	/**
	 * Date 2019-01-27 10:51:54<br>
	 * Description 获取作为邀请人的邀请列表
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage getInviteApplyList(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto query) {
		Long userId = socketSession.getKey();
		List<Map<String, Object>> list = new ArrayList<>();
		List<Long> inviterUserIds = new ArrayList<>();
		List<Long> inviteeUserIds = new ArrayList<>();
		List<Long> applyIds = new ArrayList<>();
		List<Long> groupIds = new ArrayList<>();
		query.setInviterUserId(userId);
		PageHelper.startPage(query.getCurrentPageNo(), query.getPageSize());
		List<GroupInviteApplyVo> applys = groupManageService.queryInviteRequestList(query);
		for (GroupInviteApplyVo ap : applys) {
			inviterUserIds.add(ap.getInviterUserId());
			inviteeUserIds.add(ap.getInviteeUserId());
			groupIds.add(ap.getGroupId());
			applyIds.add(ap.getId());
		}
		List<UserVo> users = userService.getUserDataList(inviterUserIds);
		Map<Long, UserVo> userMap = new HashMap<>();
		if (YitunCollectionUtil.isNotEmpty(users)) {
			for (UserVo u : users) {
				userMap.put(u.getId(), u);
			}
		}
		users = userService.getUserDataList(inviteeUserIds);
		if (YitunCollectionUtil.isNotEmpty(users)) {
			for (UserVo u : users) {
				userMap.put(u.getId(), u);
			}
		}
		if (YitunCollectionUtil.isNotEmpty(applys)) {
			for (GroupInviteApplyVo ap : applys) {
				Long inviterUserId = ap.getInviterUserId();
				Long inviteeUserId = ap.getInviteeUserId();
				Map<String, Object> map = new HashMap<>();

				UserVo inviterUser = userMap.get(inviterUserId);
				UserVo inviteeUser = userMap.get(inviteeUserId);

				map.put("apply", ap);
				map.put("inviterUser", inviterUser);
				map.put("inviteeUser", inviteeUser);

				list.add(map);
			}
		}

		ResultMessage message = new ResultMessage();
		message.bodyPut("page", new PageInfo<>(list));
		return message;
	}

	/**
	 * Date 2019-01-27 10:56:14<br>
	 * Description
	 * 请求校验
	 * @author yitun
	 * @param socketSession
	 * @param handle
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage inviteVerifyHandle(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto handle) {
		Long verifyHandleUserId = socketSession.getKey();
		Info info = groupInviteService.verifyHandle(verifyHandleUserId, handle.getApplyIds(), handle.getVerifyHandleType());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}


	/**
	 * Date 2019-01-27 10:23:38<br>
	 * Description 邀请加入
	 * @author yitun
	 * @param socketSession
	 * @param handle
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage invite(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto handle) {
		Long inviterUserId = socketSession.getKey();
		if (NumUtil.isInValid(handle.getGroupId()) || YitunCollectionUtil.isEmpty(handle.getInviteeUserIds()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Info info = groupInviteService.invite(inviterUserId, handle.getGroupId(), handle.getInviteeUserIds());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}

	/**
	 * Date 2019-01-27 10:28:43<br>
	 * Description 获取被邀请数量
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0007")
	public ResultMessage getCountByInviteeUserId(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto query) {
		query.setInviteeUserId(socketSession.getKey());
		return new ResultMessage(R.param("count", groupInviteApplyService.count(YitunMapUtil.java2Map(query, false))));
	}

	/**
	 * Date 2019-01-27 10:29:03<br>
	 * Description 获取被邀请列表
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0008")
	public ResultMessage getListByInviteeUserId(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto query) {
		query.setInviteeUserId(socketSession.getKey());
		PageHelper.startPage(query.getCurrentPageNo(), query.getPageSize());
		List<GroupInviteApplyVo> applys = groupInviteApplyService.queryInviteeList(query);

		return new ResultMessage(R.param("page", new PageInfo<>(applys)));
	}

	/**
	 * 被邀请者处理
	 * @param socketSession
	 * @param handle
	 * @return
	 */
	@ActionMapping(value = "1.1.0010")
	public ResultMessage inviteInviteeHandle(SocketSession socketSession, @JsonDefine("data") GroupInviteApplyDto handle) {
		Long inviterUserId = socketSession.getKey();
		if (null != handle.getApplyIds()) {
			for (Long applyId : handle.getApplyIds()) {
				groupInviteService.inviteeHandle(inviterUserId, applyId, handle.getInviteeHandleType());
			}
		}
		return new ResultMessage();
	}
}
